/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-20  이윤노    Created.  
*  2012-06-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	
	function clsFaqCategory(){
		this._HTMLDom = null;
		this.listData = null;
		this._init();	
	}
	
	clsFaqCategory.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.club_home_category ul.tab_menu");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("li:first")
			}	
			$layer.empty();
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();	
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			
			for(var i=0;i<this.listData.length;++i){
				$layer.append($element.clone(true));	
			}
			
			$layer.find("li").each(function(n){
				$(this).find("a").html(thisClass.listData[n]["카테고리명"]);
			}).removeClass("tab_on").eq(0).addClass("tab_on");
			
		}
		, $getFaqCategory: function(){
			return this._HTMLDom["$layer"].find("a");
		}	
		, setIdx: function(n){			
			this._HTMLDom["$layer"].find("li").removeClass("tab_on").eq(n).addClass("tab_on");
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			var idx = $layer.find("li").index($layer.find("li.tab_on"));
			return this.listData[idx]["카테고리키"];
		}
	}
	
	function clsFaqList(){
		this._HTMLDom = null;
		this.listData = null;
		this._init();	
	}
	
	clsFaqList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.club_home_category ul.tab_list");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("li:first")	
			}
			
			$layer.empty();
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();	
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.empty();
			
			for(var i=0;i<this.listData.length;++i){
				$layer.append($element.clone(true));	
			}
			
			$layer.find("li").each(function(n){				
				$(this).find("a").html("<span>Q.</span>" + thisClass.listData[n]["질문"]);				
				$(this).find("p").html("<span>A.</span>" + thisClass.listData[n]["답변"]);
			});
			
			this.setIdx(0);
		}	
		, spinStart: function(){
			
		}
		, spinEnd: function(){
			
		}
		, setIdx: function(n){
			this._HTMLDom["$layer"].find("li").removeClass("on").eq(n).addClass("on");	
			this._HTMLDom["$layer"].find("li p").hide().eq(n).show(300);
		}
		, $getBtnQuestion: function(){
			return this._HTMLDom["$layer"].find("li a");	
		}
	}
	
	function clsComm()
	{
		
		this.faqCategory = null;		
		
		this.faqList = null;
		this.faqListRequest = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){		
			this.faqListRequest = {
				"카테고리키": null	
			}
		}
//------------------------ FAQ 카테고리------------------------//	
		, getFaqCategory: function(){
			var thisClass = this;
	
			var szServicename = "getFaqCateList";
			
			// ajax 시작
			_gf.ajax({
				"sid":"QA001",
				"servicename": szServicename
			}, {
								
			}, function(ret){
				// callback and mapping
				thisClass.faqCategory = [];
									
				if(ret["msp"]["contents"]["items"]){					
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"]	= new Array(ret["msp"]["contents"]["items"]);
					}					
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){													
						var obj = ret["msp"]["contents"]["items"][i];
						if(obj["faq__detail__type"]) {
							thisClass.faqCategory.push({
								"카테고리키": _util.nvl(obj["faq__detail__type"])
								, "카테고리명": _util.nvl(obj["faq__detail__name"])
							});
						}
					}
				}
				
				$(thisClass).trigger("evtFinishFaqCategory" );
			});
		}
		, getFaqCategoryData: function(){
			return this.faqCategory;
		}
//------------------------ FAQ 카테고리------------------------//		
//------------------------ FAQ 목록------------------------//	
		, getFaqList: function(){
			var thisClass = this;
			
			var szCategory = this.faqListRequest["카테고리키"];
			var szServicename = "getFaqList";
			
			// ajax 시작
			_gf.ajax({
				"sid":"QA001",
				"servicename": szServicename,
				"listcount":999,
				"currpageno":1
			}, {
				"faq_detail_type": szCategory
			}, function(ret){
				thisClass.faqList = [];
					
				if(ret["msp"]["contents"]["items"]){					
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"]	= new Array(ret["msp"]["contents"]["items"]);
					}					
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){													
						var obj = ret["msp"]["contents"]["items"][i];
												
						thisClass.faqList.push({
							"질문":_util.nvl(obj["contents"])
							, "답변": _util.nvl(obj["reply"])
						});
					}
				}
				
				$(thisClass).trigger("evtFinishFaqList");
			});
			// ajax 끝
		}
		, getFaqListData: function(){
			return this.faqList;
		}
		, setFaqListRequest: function(key, val){
			return this.faqListRequest[key] = val;
		}
//------------------------ FAQ 목록------------------------//	
	}
	
	function clsControl()
	{
		this.objComm = null;
		
		this.objFaqCategory = null;
		this.objFaqList = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			
			this.notice_key = _util.getParam("notice_key");;
			
			this.objComm = new clsComm();
			this.objFaqCategory = new clsFaqCategory();
			this.objFaqList = new clsFaqList();
			
			this._bind();
			
			//FAQ카테고리조회
			_gf.spinStart();
			this.objComm.getFaqCategory();
			
		}
		, _bind: function(){
			var thisClass = this;
			
			//FAQ카테고리조회 완료 이벤트
			$(this.objComm).bind("evtFinishFaqCategory", function(){ 
				var thisData = thisClass.objComm.getFaqCategoryData();	
				thisClass.objFaqCategory.setList(thisData);
				_gf.spinEnd();
				
				//FAQ목록조회
				thisClass.objFaqList.spinStart();				
				thisClass.objComm.setFaqListRequest("카테고리키",thisClass.objFaqCategory.getValue());
				thisClass.objComm.getFaqList();
			});
			
			//FAQ목록조회 완료 이벤트
			$(this.objComm).bind("evtFinishFaqList", function(){ 
				var thisData = thisClass.objComm.getFaqListData();	
				thisClass.objFaqList.setList(thisData);
				thisClass.objFaqList.spinEnd();
			});
		
			//카테고리 클릭 이벤트
			this.objFaqCategory.$getFaqCategory().live("click", function(){
				var idx = thisClass.objFaqCategory.$getFaqCategory().index($(this));				
				thisClass.objFaqCategory.setIdx(idx);
				
				//FAQ목록조회
				thisClass.objFaqList.spinStart();
				thisClass.objComm.setFaqListRequest("카테고리키",thisClass.objFaqCategory.getValue());
				thisClass.objComm.getFaqList();
			});
			
			//질문 클릭 이벤트
			this.objFaqList.$getBtnQuestion().live("click", function(){
				var idx = thisClass.objFaqList.$getBtnQuestion().index($(this));
				thisClass.objFaqList.setIdx(idx);
			});
			
			//제휴문의 클릭 이벤트
			$("div#container section#main_content p.banner span a").click(function(event){
				document.location.href="partner.html";
			});
				
			//Q&A문의 클릭 이벤트
			$("div#container section#main_content h4 span a").click(function(event){
				var member_id = _util.getCookie("member_id");
				if(!member_id) {
					alert("로그인 후 이용이 가능합니다.");	
					return;
				}
				var szLink = _ROOT + "/mypage/my_qna_write.html";
				$("body").append("<a href='" + szLink + "' target='_self' id='idPermissionLink'></a>");
				$("a:last").get(0).click();//.remove();
			});
		}
		, getParam: function(n){			
			return "?notice_seq="+n +"&page="+this.objNoticeListPage.getPage();
		}	
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});	